<?php
/**
 * 检查路由绑定的 {appid} 参数是否存在对应的 authorizer
 *
 * Author: Zeon
 * Date: 2017/11/24
 * Created by Panxsoft.
 */
namespace App\Http\Requests\OpenWechat\Authorizer;

use App\Exceptions\ApiErrCode;
use App\Http\Requests\ApiRequest;
use App\Models\OpenWechat\Authorizer;

/**
 * Class RouteAppidRequest
 *
 * @property string appid
 *
 * @package App\Http\Requests\OpenWechat\Authorizer
 */
abstract class AuthorizerAppidRequest extends ApiRequest
{
    protected $auth_error_code = ApiErrCode::AUTHORIZE_ERROR_ID_APPID;

    /**
     * @var Authorizer
     */
    protected $authorizer;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $this->authorizer = Authorizer::findByAppId($this->appid);
        if (! $this->authorizer) {
            $this->setAuthError(ApiErrCode::AUTHORIZER_NOT_FOUND, "未找到指定公众号");
            return false;
        }

        if (! $this->authorizer->canOperate()) {
            $this->setAuthError(ApiErrCode::AUTHORIZE_CANCELED, "公众号已取消订阅");
            return false;
        }

        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}
